package com.example.smartcity_poverty.ui.AllService.LivingPayment.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.context
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_lp_add.*
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 * 交费页面
 */
class LpAddActivity : AppCompatActivity() {

    private val phoneName by lazy { intent.getStringExtra("phoneName") } // 手机卡运营商
    private val phoneNumber by lazy { intent.getStringExtra("phoneNumber") } // 手机号码

    private var phoneNumberSize: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_lp_add)
        title = "交费页面"

        lpAddPhoneNumber.text = "缴费手机号: ${phoneNumber}"
        lpAddPhoneName.text = "运营商: ${phoneName}"



        lpAddMoney50.setOnClickListener {
            lpAddMoneySize.text = "缴费金额: ${lpAddMoney50.text}"
            phoneNumberSize = lpAddMoney50.text.toString()
        }

        lpAddMoney100.setOnClickListener {
            lpAddMoneySize.text = "缴费金额: ${lpAddMoney100.text}"
            phoneNumberSize = lpAddMoney100.text.toString()
        }

        lpAddMoney200.setOnClickListener {
            lpAddMoneySize.text = "缴费金额: ${lpAddMoney200.text}"
            phoneNumberSize = lpAddMoney200.text.toString()
        }

        lpAddOk.setOnClickListener {
            if (phoneNumberSize == null) {
                "请选择金额！".show()
            } else {
                // 缴费
                coroutine(Dispatchers.Main) {
                    api.getLpAddPhone(getToken(), JSONObject().apply {
                        put("paymentType", "电子支付")
                        put("phonenumber", phoneNumber.toString())
                        put("rechargeAmount", phoneNumberSize.toString())
                        put("type", 1)
                    }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                        if (this.code == 200) {
                            runOnUiThread {
                                startActivity(Intent(context, LpAddTrueActivity::class.java).apply {
                                    putExtra("phone", phoneNumber)
                                })
                            }
                        } else runOnUiThread { this.msg?.show() }
                    }
                }
            }

        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}